/*********************************************************************************
 *      Copyright:  (C) 2022 Zhang Changxing<ZhangChangxingVIP@163.com>
 *                  All rights reserved.
 *
 *       Filename:  transf_format.c
 *    Description:  This file  is interchangeable between string format and CJSON format.
 *                 
 *        Version:  1.0.0(05/1/2022)
 *         Author:  Zhang Changxing <ZhangChangxingVIP@163.com>
 *      ChangeLog:  1, Release initial version on "05/1/2022  01:33:25 AM"
 *                 
 ********************************************************************************/
#include "transf_format.h"


/*--------------------------------------------------------------------------------------------------
 *   description:   Convert string format to CJSON forma
 *          args:   $mqtt:      The configuration information structure pointer that connects the broker
 *                  $pack_info: The structure of the packet to be sent
 *  return value:      <0: failure,    =0:ok 
 *--------------------------------------------------------------------------------------------------*/
int pack_to_cjson(pub_ctx_t *pub_ctx, pack_info_t pack_info)
{
    char *str;

     if(!pub_ctx)
    {
        log_error("The %s() arguments input error!\n", __func__);
        return -1;
    }   

    log_debug("platform_flag:%d\n", pub_ctx->platform_flag);
    memset(pub_ctx->pub_message, 0, sizeof(pub_ctx->pub_message));
    

    if(ALI == pub_ctx->platform_flag)
    {
        //Ali
        cJSON *temp_item = cJSON_CreateObject();
        cJSON_AddNumberToObject(temp_item, "Temperature", pack_info.temper);
        cJSON *ack_item = cJSON_CreateObject();
        //cJSON_AddNumberToObject(ack_item, "ack", 0);

        cJSON*root = cJSON_CreateObject();
        cJSON_AddStringToObject(root, "method","thing.service.property.post");
        cJSON_AddStringToObject(root, "id",pack_info.devid);
        //cJSON_AddItemToObject(root, "sys",ack_item);
        cJSON_AddItemToObject(root, "params",temp_item);
        cJSON_AddStringToObject(root, "version","1.0.0");

        str = cJSON_Print(root);
        strncpy(pub_ctx->pub_message, str, strlen(str));
    }
    else if(TENGCENT == pub_ctx->platform_flag)
    {
        //tencent
        cJSON *item = cJSON_CreateObject();
        cJSON_AddNumberToObject(item, "temperature",pack_info.temper);

        cJSON*root = cJSON_CreateObject();
        cJSON_AddStringToObject(root, "method","report");
        cJSON_AddStringToObject(root, "clientToken","clientToken-1272ba56-8b7a-4297-ba5d-324b0da70789");
        cJSON_AddItemToObject(root, "params",item);

        str = cJSON_Print(root);
        strncpy(pub_ctx->pub_message, str, strlen(str));
    }
    else
    {
        //huawei
        cJSON *item = cJSON_CreateObject();
        cJSON_AddNumberToObject(item, "temp",pack_info.temper);
        cJSON *array = cJSON_CreateArray();
        cJSON *array_obj =  cJSON_CreateObject();
        cJSON_AddStringToObject(array_obj, "service_id","temperature");
        cJSON_AddItemToArray(array, array_obj);
        cJSON_AddItemToObject(array_obj, "properties", item);
        cJSON *root = cJSON_CreateObject();
        cJSON_AddItemToObject(root, "services",array);

        str = cJSON_Print(root);
        strncpy(pub_ctx->pub_message, str, strlen(str));

    }
    

    return 0;
}


/*--------------------------------------------------------------------------------------------------
 *   description:   Parse the CJSON format to a string format
 *          args:   $mqtt:    The configuration information structure pointer that connects the broker
 *  return value:      <0: failure,    =0:ok 
 *--------------------------------------------------------------------------------------------------*/
int cjson_parse(sub_ctx_t *sub_ctx)
{
    if(!sub_ctx)
    {
        log_error("The %s() arguments input error!\n", __func__);
        return -1;
    }

    if(0 == sub_ctx->platform_flag)
    {
        //Ali
        cJSON * cjson = cJSON_Parse(sub_ctx->sub_message);
        if(NULL == cjson)
        {
            log_error("Json pack into cjson failure!\n");
            return -1;
        }
        cJSON_Print(cjson);
        cJSON *params = cJSON_GetObjectItem(cjson, "params");
        log_info("Succeeded in obtaining subscription data:Temperature  = %s\n\n\n",cJSON_Print(cJSON_GetObjectItem(params, "Temperature")));
    }
    else if(1 == sub_ctx->platform_flag)
    {
        //tencent
        cJSON * cjson = cJSON_Parse(sub_ctx->sub_message);
        if(NULL == cjson)
        {
            log_error("Json pack into cjson failure!\n");
            return -1;
        }
        cJSON_Print(cjson);
        char *str1 = cJSON_Print(cJSON_GetObjectItem(cjson, "method"));
        char *str2 = "\"control\"";
        if(strncmp(str1, str2, strlen(str2)))
        {
           return  0;
        }
        
        cJSON *params = cJSON_GetObjectItem(cjson, "params");
        log_info("Succeeded in obtaining subscription data:temperature = %s\n\n\n",cJSON_Print(cJSON_GetObjectItem(params, "temperature")));
    }
    else
    {
        //huawei
        cJSON * cjson = cJSON_Parse(sub_ctx->sub_message);
        if(NULL == cjson)
        {
            log_error("Json pack into cjson failure!\n");
            return -1;
        }
        cJSON_Print(cjson);
        cJSON *paras = cJSON_GetObjectItem(cjson, "paras");
        log_info("Succeeded in obtaining subscription data:temperature = %s\n\n\n",cJSON_Print(cJSON_GetObjectItem(paras, "temperature")));
    }

    return 0;
}